package com.jwt.demo.controller;

import com.jwt.demo.annotation.PassToken;
import com.jwt.demo.domain.User;
import com.jwt.demo.service.impl.TokenService;
import com.jwt.demo.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisUtil redisUtil;
    @RequestMapping("/login")
    @PassToken
    public Object login(@RequestBody User user){
        user.setId(1);
        String token = tokenService.getToken(user);
        redisUtil.setCacheObject(token,user);
        Map<String,String> resultInfo = new HashMap<>();
        resultInfo.put("ststus","200");
        resultInfo.put("username",user.getUsername());
        resultInfo.put("password",user.getPassword());
        resultInfo.put("token",token);
        return resultInfo;
    }
}
